package com.zygh.policeasset.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.OrderReply;
import com.zygh.policeasset.pojo.Repair;
import com.zygh.policeasset.pojo.vo.RepairVO;
import com.zygh.policeasset.service.RepairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = {"故障上报管理"})
@RequestMapping({"/repair"})
@RestController
public class RepairController {
    @Resource
    RepairService repairService;

    @GetMapping({"/selectOne"})
    @ApiOperation("通过ID查询")
    public RepairVO selectOne(Integer id) {
        return this.repairService.queryById(id);
    }

    @GetMapping({"/queryAll"})
    @ApiOperation("分页查询全部")
    public PageInfo queryAll(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                             @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize,
                             @RequestParam(value = "status", required = false) Integer status,
                             @RequestParam(value = "orderStatus", required = false) Integer orderStatus,
                             @RequestParam(value = "orderUserId", required = false) Integer orderUserId,
                             @RequestHeader(value = "findAll", required = false) boolean findAll,
                             @RequestHeader(value = "userId", required = false) Integer userId) {
        if (findAll) {
            return this.repairService.queryAll(pageNum.intValue(), pageSize.intValue(), null, status, orderStatus, orderUserId);
        }
        return this.repairService.queryAll(pageNum.intValue(), pageSize.intValue(), userId, status, orderStatus, orderUserId);
    }

    @GetMapping({"/queryByOrderUserId"})
    @ApiOperation("根据用户查询")
    public List<RepairVO> queryByOrderUserId(@RequestParam(value = "orderUserId", required = false) Integer orderUserId) {
        return this.repairService.queryByOrderUserId(orderUserId);
    }

    @GetMapping({"/queryByIds"})
    @ApiOperation("根据用户查询")
    public List<RepairVO> queryByIds(@RequestParam(value = "ids", required = false) String ids) {
        return this.repairService.queryByIds(ids);
    }

    @GetMapping({"/appQueryOrder"})
    @ApiOperation("工单查询")
    public PageInfo appQueryOrder(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                                  @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize,
                                  @RequestParam(value = "status", required = false) Integer status,
                                  @RequestParam(value = "orderStatus", required = false) Integer orderStatus,
                                  @RequestParam(value = "orderUserId", required = false) Integer orderUserId,
                                  @RequestHeader(value = "findAll", required = false) boolean findAll,
                                  @RequestHeader(value = "userId", required = false) Integer userId) {
        if (findAll) {
            return this.repairService.appQueryOrder(pageNum.intValue(), pageSize.intValue(), null, status, orderStatus, null);
        }
        return this.repairService.appQueryOrder(pageNum.intValue(), pageSize.intValue(), userId, status, orderStatus, orderUserId);
    }

    @PostMapping({"/insert"})
    @ApiOperation("添加")
    public CommonResult insert(@RequestBody Repair repair) {
        return this.repairService.insert(repair);
    }

    @PostMapping({"/update"})
    @ApiOperation("修改")
    public CommonResult update(@RequestBody Repair repair) {
        return this.repairService.update(repair);
    }

    @DeleteMapping({"/delete/{id}"})
    @ApiOperation("根据id删除")
    public CommonResult delete(@PathVariable Integer id) {
        return this.repairService.deleteById(id);
    }

    @PostMapping({"/issuedOrder"})
    @ApiImplicitParams({@ApiImplicitParam(name = "json", value = "id是故障表主键，userId是下发给谁，以json格式传", required = true, paramType = "query")})
    @ApiOperation("下发工单")
    public CommonResult issuedOrder(@RequestBody JSONObject json) {
        return this.repairService.issuedOrder(json.getInteger("id"), json.getInteger("userId"));
    }

    @PostMapping({"/orderOperation"})
    @ApiImplicitParams({@ApiImplicitParam(name = "json", value = "id是故障表主键，以json格式传", required = true, paramType = "query")})
    @ApiOperation("接工单")
    public CommonResult orderOperation(@RequestBody JSONObject json) {
        return this.repairService.orderOperation(json.getInteger("id"));
    }

    @PostMapping({"/orderReply"})
    @ApiOperation("回工单")
    public CommonResult orderReply(@RequestBody OrderReply orderReply) {
        return this.repairService.orderReply(orderReply);
    }

    @PostMapping({"/sealOnOrder"})
    @ApiOperation("打印派工单")
    public CommonResult sealOnOrder(@RequestBody String[] ids) {
        return this.repairService.sealOnOrder(ids);
    }
}